package 力扣日常刷题.木22二月.第13天219;


/**
 * @author 帅小伙
 * @date 2022/2/19
 * @description
 */
public class Demo97轮转数组 {


    // 额外数组
    public void rotate(int[] nums, int k) {
        int[] arr = new int[nums.length];
        for (int i = 0; i < nums.length; i++)
            arr[(i+k)%nums.length] = nums[i];
        System.arraycopy(arr, 0,nums,0,nums.length);
    }


    // 数组反转
    /*
    * 反转所有元素
    * 反转 0 , k - 1
    * 反转 k, n - 1;
    * */

    public void rotate2(int[] nums, int k) {
        k %= nums.length;
        reverse(nums,0,nums.length-1);
        reverse(nums,0,k - 1);
        reverse(nums,k,nums.length - 1);
    }

    public void reverse(int[] nums,int start,int end) {
        while(start< end) {
            int t = nums[start];
            nums[start] = nums[end];
            nums[end] = t;
            start++;
            end--;
        }
    }

}
